<?
function GetVisitDetails1($client_id)
{
	global $sqlForYear;
	$queryVisitDate = "SELECT * 
						FROM er_page2 
						LEFT OUTER JOIN er_visit 
						ON er_page2.visit_id = er_visit.visit_id 
						WHERE er_page2.client_id='258' 
						AND er_visit.visit_date >= ".$sqlForYear." 
						ORDER BY er_visit.visit_id";
					   
	$resultVisit = r_db($queryVisitDate);
	return $resultVisit;
}


function getNoOfVisits1($client_id)
{
	$queryNoOfVisits = "SELECT client_id, COUNT(visit_id)
						FROM er_page2
						WHERE client_id = $client_id
						GROUP BY client_id";

	$resultNoOfVisits = r_db($queryNoOfVisits);
	$rowNoOfVisits = mysql_fetch_array($resultNoOfVisits);
	$noOfVisits = $rowNoOfVisits[1];
	return $noOfVisits;
}

function getVisit_id($client_id)
{
	global $sqlForYear;
	$queryVisit_id = "SELECT *
					  FROM er_visit
					  WHERE client_id = '$client_id'
					  AND visit_date >= ".$sqlForYear."
					  ORDER BY visit_id";

	$resultVisit_id = r_db($queryVisit_id);
	return $resultVisit_id;
}

function getIDtype($idtype)
{
	$query="select identification_type from er_Identification_Type where identification_no='".$idtype."'";
	$resultIDtype = r_db($query);
	$row=mysql_fetch_array($resultIDtype);
	return $row;
}

if (isset($_POST['clientID'])) {
	$client_id = $HTTP_POST_VARS['clientID'];
	createSessionClientId($client_id);
	print_r($_POST);
}
elseif (isset($_GET['clientID'])) {
	$client_id = $HTTP_GET_VARS['clientID'];
	createSessionClientId($client_id);
}
elseif (isset($_SESSION['session_clientId'])) {
	$client_id = $_SESSION['session_clientId'];
}
?>

                <div id="tabs10">
                <ul>
                                <!-- CSS Tabs -->
<? echo "<li><a href=\"?page=visit_details&client_id=$client_id\"><span>Page 1</span></a></li>"; ?>
<? echo "<li><a href=\"?page=view_page_2&client_id=$client_id\"><span>Page 2</span></a></li>"; ?>
                </ul>
                </div>
<SCRIPT LANGUAGE="JavaScript">
<!--
function OpenWindow(url, name, width, height)
{
	popupWin = window.open(url, '', 'width=' + width + ', height=' + height + ',scrollbars,resizable')
}
-->
</script>
<?

if (isset($client_id))
{
	$query = "SELECT * FROM er_Client WHERE client_id ='$client_id'";
	$sql = r_db($query);
	$row = mysql_fetch_array($sql);
	$idtyperesult=getIDtype($row[10]);
	echo "<table border=\"0\" width=\"100%\" id=\"visit\" cellpadding=\"2\" cellspacing=\"0\"> ";
	echo "<tr><th colspan=\"2\" align=\"left\">Client Details</th><th colspan=\"2\" align=\"right\"><a href=\"javascript:void(OpenWindow('pages/form_visit_details_page2.php?ClientID=$client_id','',710, 400));\">Print Client's Visit Details</a><br /><i>Clients with over 8 visits may not print correctly</i></th></tr>";
	echo  "<tr><td width=\"20%\">Surname:</td><td width=\"30%\">$row[2]</td><td width=\"20%\">Given Name:</td><td width=\"30%\">".$row[3]."</td></tr>";

	$DOB =  $row[4];
	$DbDOB=explode("-",$DOB);
	$dayDOB=$DbDOB[2];
	$monthDOB=$DbDOB[1];
	$yearDOB=$DbDOB[0];
	$DOB=$dayDOB."/".$monthDOB."/".$yearDOB;
	
	if ($row[6]=="f")
		$gender="Female";
	else
		$gender="Male";
	echo  "<tr>"
	.	"<td>Date of Birth:</td><td>" . $DOB."</td>";
	echo "<td>Gender:</td><td>$gender</td>";
	echo  "</tr>";

	echo "<tr>"
	.	"<td>ID Type:<td>".$idtyperesult[0]. "</td>"
	.     "<td>ID Reference No:</td><td>" . $row[1]."</td>";
	echo "</tr>";
	$otheridvalue = $row[8];
	if ($otheridvalue != null)
	{
		echo   "<tr><td>&nbsp;</td><td>&nbsp;</td><td>Other ID Type:</td><td>" . $row[8]."</td></tr>";
	}

	print "   <input type=\"hidden\" name=\"selectedClientID\" value=\"". $row[0] . "\" />";
	print "   <input type=\"hidden\" name=\"selectedClientSurname\" value=\"". $row[2] . "\" />";
	print "   <input type=\"hidden\" name=\"selectedClientFirstName\" value=\"". $row[3] . "\" />";


	echo "</table>";
}


if ($client_id > NULL)
	{
		$result = getVisitDetails1($client_id);
		$i = 0;
		$visit = array();

		while ($temp = mysql_fetch_array($result))
		{
			$visit[$i] = $temp;
			$i++;
		}

		$result1 = getVisit_id($client_id);
		$x = 0;
		$dates = array();

		while ($temp1 = mysql_fetch_array($result1))
		{
			$dates[$x] = $temp1;
			$x++;
		}

	}

$noOfVisits = getNoOfVisits1($client_id);
if ($noOfVisits > 0) {
?>
	<Br />
<table id="visit" width="100%" cellpadding="2" cellspacing="0">
	<tr>
	<th>Current Year Visit(s)</th>
	<?
	foreach ($visit as $v => $value) {
		if ($v < 3) {
			echo "<th class=\"green\">Visit #". ($v+1) ."</th>";
		}
		elseif ($v < 4) {
			echo "<th class=\"orange\">Visit #". ($v+1) ."</th>";
		}
		else {
			echo "<th class=\"red\">Visit #". ($v+1) ."</th>";
		}
	}
	?>
	</tr>

	<tr><td valign="top">Date</td>
			<?
			foreach ($dates as $v2 => $value2) {
				$DbDOB=explode("-",$value2['visit_date']);
				@$dayDOB=$DbDOB[2];
				@$monthDOB=$DbDOB[1];
				$yearDOB=$DbDOB[0];
				$dobDate=$dayDOB."/".$monthDOB."/".$yearDOB;
				echo "<td>$dobDate</td>";
			}
			?>
	</tr>

	<tr>
	<th>Presenting Issues</th>
	</tr>

	<tr><td valign="top" width="20%">Presenting Issue 1</td>
		<?
			foreach ($visit as $v => $value) {
				echo "<td>".$value['pres_is_1']."</td>";
			}
		?>
	</tr>
	<tr><td valign="top">Presenting Issue 2</td>
		<?
			foreach ($visit as $v => $value) {
				echo "<td>".$value['pres_is_2']."</td>";
			}
		?>
	</tr>
	<tr><td valign="top">Presenting Issue 3</td>
		<?
			foreach ($visit as $v => $value) {
				echo "<td>".$value['pres_is_3']."</td>";
			}
		?>
	</tr>
	<tr><td valign="top">Presenting Issue 4</td>
		<?
			foreach ($visit as $v => $value) {
				echo "<td>".$value['pres_is_4']."</td>";
			}
		?>
	</tr>


	<tr>
		<th>Options Discussed</th>
	</tr>
	<tr><td valign="top">Options Discussed 1</td>
		<?
			foreach ($visit as $v => $value) {
				echo "<td>".$value['opt_1']."</td>";
			}
		?>
	</tr>
	<tr><td valign="top">Options Discussed 2</td>
		<?
			foreach ($visit as $v => $value) {
				echo "<td>".$value['opt_2']."</td>";
			}
		?>
	</tr>
	<tr><td valign="top">Options Discussed 3</td>
		<?
			foreach ($visit as $v => $value) {
				echo "<td>".$value['opt_3']."</td>";
			}
		?>
	</tr>
	<tr><td valign="top">Options Discussed 4</td>
		<?
			foreach ($visit as $v => $value) {
				echo "<td>".$value['opt_4']."</td>";
			}
		?>
	</tr>


	<tr>
		<th>Case Worker Details</th>
	</tr>
	<tr><td valign="top">Does this client have a caseworker?</td>
		<?
			foreach ($visit as $v => $value) {
				echo "<td>".$value['casew']."</td>";
			}
		?>
	</tr>
	<tr><td valign="top">Caseworker name:</td>
		<?
			foreach ($visit as $v => $value) {
				echo "<td>".$value['casew_name']."</td>";
			}
		?>
	</tr>
	<tr><td valign="top">Caseworkers Organisation</td>
		<?
			foreach ($visit as $v => $value) {
				echo "<td>".$value['casew_org']."</td>";
			}
		?>
	</tr>

		<tr>
			<th>Client Signature</th>
		</tr>
		<tr><td valign="top">Did the client provide a signature?</td>
			<?
				foreach ($visit as $v => $value) {
					echo "<td>".$value['client_sig']."</td>";
				}
			?>
		</tr>
		<tr><td valign="top">Time taken: (h:m)</td>
			<?
				foreach ($visit as $v => $value) {
					echo "<td>".$value['time_taken']."</td>";
				}
			?>
		</tr>
		<tr><td valign="top">Duty worker:</td>
			<?
				foreach ($visit as $v => $value) {
					echo "<td>".$value['duty_worker']."</td>";
				}


	echo "</tr>";
	echo "</table>";

	}
	else {
		echo "<p>This client has no visits within the last year.</p>";
	}
?>


